import React, {Component} from 'react';
import {View, AppRegistry, Navigator} from 'react-native';
import router from './router';
import './util/polyfill';
import {getTotal, initialData} from './actions/overtimeAction';

const INITIAL_COUNT = -9999;
export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {total: INITIAL_COUNT};
    initialData(() => {
      getTotal((total) => {
        this.setState({total: parseFloat(total) || 0});
      })
    });
  }

  updateTotal(total) {
    this.setState({total});
  }

  render() {
    return (
      <Navigator
        initialRoute={{path: "/", component: router['/']}}
        configureScene={() => Navigator.SceneConfigs.FloatFromRight}
        renderScene={(route, navigator) => {
          const Component = router[route.path];
          return (
            <Component
              {...route}
              navigator={navigator}
              total={this.state.total}
              updateTotal={this.updateTotal.bind(this)}
            />
          )
        }}>
      </Navigator>
    );
  }
}

AppRegistry.registerComponent('App', () => App);
